package com.appspot.finajjarane.framework.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.appspot.finajjarane.framework.models.ArticleModel;
import com.appspot.finajjarane.framework.service.IImageService;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.Image;

@Service
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class ImageService implements IImageService {

	private BlobstoreService blobstoreService;

	public ImageService(){
		this.blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	}

	@Override
	public Image getImageByArticle(ArticleModel article) {



		return null;
	}

	@Override
	public BlobKey uploadImageForArticle(ArticleModel article) {

		return null;
	}

	@Override
	public String createUploadUrl(String baseUrl) {
		return blobstoreService.createUploadUrl(baseUrl);
	}

	@Override
	public String getImageKeyUploaded(HttpServletRequest request, String fileFormName) {

		try {
		Map<String, List<BlobKey>> blobMap = blobstoreService.getUploads(request);

		return blobMap.get(fileFormName).get(0).getKeyString();

		} catch (Exception e) {
			return null;
		}



	}



}
